﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YJKInspectModel.ModelValidation.Utils;

namespace YJKInspectModel.ModelValidation.Walls
{
	public partial class WallsValidationSettingForm : System.Windows.Forms.Form
	{
		public WallsValidationSettingForm.SelectType WallSelectType
		{
			get
			{
				if (this.rdbByBox.Checked)
				{
					return WallsValidationSettingForm.SelectType.ST_ByBox;
				}
				if (this.rdbByLayer.Checked)
				{
					return WallsValidationSettingForm.SelectType.ST_ByLayer;
				}
				return WallsValidationSettingForm.SelectType.ST_ByProject;
			}
		}

		public bool IsExtendLength
		{
			get
			{
				return this.rdbExtendLength.Checked;
			}
		}

		public double ExtendLength
		{
			get
			{
				return Common.MMToFeet(Convert.ToDouble(this.m_ExtendLength));
			}
		}

		public int WallWidthMultiple
		{
			get
			{
				return this.m_WallWidthMultiple;
			}
		}

		public WallsValidationSettingForm()
		{
			this.InitializeComponent();
		}

		private void OffsetTestForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.ReadData();
		}

		private void OffsetTestForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (((System.Windows.Forms.Form)sender).DialogResult == DialogResult.OK && !this.ExtendLengthValueIsValidating())
			{
				e.Cancel = true;
				MessageBox.Show("'绝对值' 必须大于零", "盈建科提示");
				return;
			}
			this.m_WallWidthMultiple = Convert.ToInt32(this.nudWallWidthMultiple.Value.ToString());
			this.SaveData();
		}

		private void ReadData()
		{
			string path = Path.GetTempPath() + "WallsValidationSettingForm.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				this.m_ExtendLength = "500";
				this.tbExtendLength.Text = this.m_ExtendLength;
				this.m_WallWidthMultiple = 1;
				this.nudWallWidthMultiple.Value = this.m_WallWidthMultiple;
				this.rdbByBox.Checked = true;
				this.rdbByLayer.Checked = false;
				this.rdbByProject.Checked = false;
				this.rdbWidthMultiple.Checked = true;
				this.rdbExtendLength.Checked = false;
				return;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int x = Convert.ToInt32(streamReader.ReadLine());
			int y = Convert.ToInt32(streamReader.ReadLine());
			base.Location = new System.Drawing.Point(x, y);
			this.rdbByBox.Checked = Convert.ToBoolean(streamReader.ReadLine());
			this.rdbByLayer.Checked = Convert.ToBoolean(streamReader.ReadLine());
			this.rdbByProject.Checked = Convert.ToBoolean(streamReader.ReadLine());
			this.rdbWidthMultiple.Checked = Convert.ToBoolean(streamReader.ReadLine());
			this.rdbExtendLength.Checked = Convert.ToBoolean(streamReader.ReadLine());
			this.m_ExtendLength = streamReader.ReadLine();
			this.tbExtendLength.Text = this.m_ExtendLength;
			this.m_WallWidthMultiple = Convert.ToInt32(streamReader.ReadLine());
			this.nudWallWidthMultiple.Value = this.m_WallWidthMultiple;
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "WallsValidationSettingForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.rdbByBox.Checked);
			streamWriter.WriteLine(this.rdbByLayer.Checked);
			streamWriter.WriteLine(this.rdbByProject.Checked);
			streamWriter.WriteLine(this.rdbWidthMultiple.Checked);
			streamWriter.WriteLine(this.rdbExtendLength.Checked);
			streamWriter.WriteLine(this.m_ExtendLength);
			streamWriter.WriteLine(this.m_WallWidthMultiple);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private bool ExtendLengthValueIsValidating()
		{
			bool result;
			try
			{
				if (!Geometry.GreaterThan(double.Parse(this.tbExtendLength.Text), 0.0))
				{
					this.tbExtendLength.Text = this.m_ExtendLength;
					result = false;
				}
				else
				{
					this.m_ExtendLength = this.tbExtendLength.Text;
					result = true;
				}
			}
			catch (Exception)
			{
				this.tbExtendLength.Text = this.m_ExtendLength;
				result = false;
			}
			return result;
		}

		private void rdbWidthMultiple_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdbWidthMultiple.Checked)
			{
				this.nudWallWidthMultiple.Enabled = true;
				this.tbExtendLength.Enabled = false;
				return;
			}
			this.nudWallWidthMultiple.Enabled = false;
			this.tbExtendLength.Enabled = true;
		}

		private void rdbExtendLength_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdbExtendLength.Checked)
			{
				this.nudWallWidthMultiple.Enabled = false;
				this.tbExtendLength.Enabled = true;
				return;
			}
			this.nudWallWidthMultiple.Enabled = true;
			this.tbExtendLength.Enabled = false;
		}

		private string m_ExtendLength = "";

		private int m_WallWidthMultiple = 1;

		public enum SelectType
		{
			ST_ByBox,
			ST_ByLayer,
			ST_ByProject
		}
	}
}
